<template>
  <div class="chat-list">
    <div class="header">
      <span class="title">会话列表</span>
      <button class="new-btn" @click="$emit('new')">+ 新建会话</button>
    </div>
    <div class="list">
      <div
        v-for="item in conversations"
        :key="item.id"
        :class="['item', { active: item.id === activeId }]"
        @click="select(item.id)"
      >
        <div class="item-row-top">
          <span class="item-title">{{ item.name || item.id }}</span>
          <div class="item-actions">
            <button class="rename-btn action-btn" @click.stop="rename(item)">✏️</button>
            <button class="delete-btn action-btn" @click.stop="del(item)">🗑️</button>
          </div>
        </div>
        <div class="item-row-bottom">
          <span class="item-time">{{ formatTime(item.created_at) }}</span>
        </div>
      </div>
      <div v-if="!conversations.length" class="empty">暂无会话</div>
    </div>
  </div>
</template>

<script setup>
import {getMessages, renameConversation} from "../api";

const props = defineProps({
  conversations: Array,
  activeId: String,
  conversationId: String,
  messages: Array
})
const emit = defineEmits(['select', 'rename', 'delete'])

function formatTime(ts) {
  if (!ts) return ''
  const d = new Date(ts * 1000)
  return d.toLocaleString()
}
function rename(item) {
  emit('rename', item)
}
function del(item) {
  emit('delete', item)
}
function select(conversation_id) {
  emit('select', conversation_id)
}
</script>

<style scoped>
.chat-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #f5f6fa;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 10px 20px;
  border-bottom: 1px solid #eee;
  background: #fafbfc;
}
.title {
  font-weight: bold;
  font-size: 17px;
}
.new-btn {
  background: #1677ff;
  color: #fff;
  border: none;
  border-radius: 16px;
  padding: 4px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}
.new-btn:hover {
  background: #409eff;
}
.list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 0;
}
.item {
  margin: 10px 12px;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.03);
  padding: 14px 18px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: background 0.2s, box-shadow 0.2s, border 0.2s;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.item:not(:last-child) {
  margin-bottom: 10px;
}
.item:hover {
  background: #f0f6ff;
  box-shadow: 0 4px 16px rgba(22,119,255,0.08);
  border: 2px solid #a3c8fa;
}
.item.active {
  background: #e6f2ff;
  border: 2.5px solid #1677ff;
  box-shadow: 0 4px 18px rgba(22,119,255,0.13);
}
.item-row-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}
.item-title {
  font-size: 15px;
  font-weight: 500;
  color: #222;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
.item-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  margin-left: 12px;
}
.item:hover .item-actions {
  opacity: 1;
  pointer-events: auto;
}
.item-row-bottom {
  margin-top: 2px;
}
.item-time {
  font-size: 12px;
  color: #a0a0a0;
}
.action-btn {
  background: rgba(22,119,255,0.08);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 15px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.18s, color 0.18s, box-shadow 0.18s;
  color: #1677ff;
  box-shadow: 0 1px 2px rgba(22,119,255,0.04);
}
.action-btn:hover {
  background: #1677ff;
  color: #fff;
  box-shadow: 0 2px 8px rgba(22,119,255,0.13);
}
.empty {
  text-align: center;
  color: #bbb;
  margin-top: 40px;
}
</style> 